const openid = getApp().globalData.openid;

Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 顶部导航条
        tabs: [{
                id: 0,
                name: "买菜",
                isActive: "active"
            },
            {
                id: 1,
                name: "点餐",
                isActive: ""
            },
            {
                id: 2,
                name: "随机点餐",
                isActive: ""
            }
        ],
        // 切换菜单项时列表滚动位置置顶
        scrollTop: 0,
        // 要显示的已选列表
        choosed: [],
        // 全部已选列表
        choosedList: [],
        //当前选中项
        currentIndex: 0,
        //记录是否是第一次打开该页面
        isFirstOpen: true,
        //只有买菜页面需要显示inputbox组件
        inputboxDisplay: "block",
        detailDisplay: "none"
    },

    //导航栏选中样式并显示对应的页面
    onItemChange(e) {
        let {
            tabs
        } = this.data;
        tabs.forEach((value, index) => {
            if (index == e.detail) {
                value.isActive = "active";
                if (index != 0) {
                    this.setData({
                        inputboxDisplay: "none",
                        detailDisplay: "block"
                    })
                } else {
                    this.setData({
                        inputboxDisplay: "block",
                        detailDisplay: "none"
                    })
                }
            } else {
                value.isActive = "";
            }
        });
        this.setData({
            tabs: tabs,
            scrollTop: 0,
            currentIndex: e.detail,
            choosed: this.data.choosedList[e.detail]
        });
    },

    //长按可以通过多选框删除
    // deleteMemoItem(e) {

    // },

    //点击按钮删除某项记录
    delete(e) {
        //从数组中删去这一项并保存到缓存
        let choosedList = this.data.choosedList;
        choosedList[this.data.currentIndex].splice(e.currentTarget.dataset.index, 1);
        this.setData({
            choosedList: choosedList,
            choosed: choosedList[this.data.currentIndex]
        });
        switch (this.data.currentIndex) {
            case 0 :
                wx.setStorageSync('buyChoosed', choosedList[0]);
                break;
            case 1 :
                wx.setStorageSync('menuChoosed', choosedList[1]);
                break;
            case 2 :
                wx.setStorageSync('randomMenuChoosed', choosedList[3]);
                break;
        }
        
        wx.showToast({
            title: '删除成功',
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (!wx.getStorageSync('buyChoosed')) {
            try {
                wx.cloud.database().collection("buyChoosed").doc(openid).get().then(res => {
                    let buyChoosed = res.data.buyChoosed;
                    wx.setStorageSync('buyChoosed', buyChoosed);
                    this.setData({
                        choosed: buyChoosed
                    })
                });
            } catch (err) {
                console.log(err);
            }
        }
        if (!wx.getStorageSync('menuChoosed')) {
            try {
                wx.cloud.database().collection("menuChoosed").doc(openid).get().then(res => {
                    wx.setStorageSync('menuChoosed', res.data.menuChoosed);
                });
            } catch (err) {
                console.log(err);
            }
        }
        if (!wx.getStorageSync('randomMenuChoosed')) {
            try {
                wx.cloud.database().collection("randomMenuChoosed").doc(openid).get().then(res => {
                    wx.setStorageSync('randomMenuChoosed', res.data.randomMenuChoosed);
                });
            } catch (err) {
                console.log(err);
            }
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //加载数据方法
        var that = this;

        function getData() {
            let choosedList = [];
            choosedList.push(wx.getStorageSync("buyChoosed"));
            choosedList.push(wx.getStorageSync("menuChoosed"));
            choosedList.push(wx.getStorageSync("randomMenuChoosed"));
            that.setData({
                choosedList: choosedList,
                choosed: choosedList[that.data.currentIndex]
            });
        }

        //如果是第一次打开该页面就先等2秒钟加载数据，避免缓存被清除后onload还没从云端异步加载好数据就执行onshow，打开是空白页面
        if (this.data.isFirstOpen) {
            setTimeout(() => {
                getData();
            }, 2000)
            this.data.isFirstOpen = false;
        } else {
            getData();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (options) {
        var shareObj = {
            title: "吃啥子！",
            desc: "要买这些/想吃这些",
            imageUrl: this.data.choosedList[0].imageUrl
        }
        return shareObj;
    }
})